package com.sunsy.authserver.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sunsy.authserver.constants.ApiConstants.PO;
import com.sunsy.authserver.constants.UserContextHolder;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * mybatis新增、更新填充
 *
 * @author 孙思远
 * @date 2021/7/19
 */
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    /**
     * 填充新增字段（源码不可有默认值，会不填充）
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        String userName = UserContextHolder.getInstance().getUserName();
        //需要强制置空 四个字段防止 误提交
        this.strictInsertFill(metaObject, PO.CREATED_BY_SIMPLE_NAME, String.class, userName);
        this.strictInsertFill(metaObject, PO.CREATED_TIME_SIMPLE_NAME, Date.class,new Date());
        this.strictInsertFill(metaObject, PO.UPDATED_BY_SIMPLE_NAME, String.class, userName);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, PO.UPDATED_BY_SIMPLE_NAME, String.class,
                UserContextHolder.getInstance().getUserName());
        this.strictUpdateFill(metaObject, PO.UPDATED_TIME_SIMPLE_NAME,Date.class,new Date());
    }
}
